package com.lz.ccjdemo.greendao;

import com.lz.ccjdemo.App;
import com.lz.ccjdemo.greendao.dao.DaoSession;
import com.lz.ccjdemo.greendao.dao.DishDataDao;
import com.lz.ccjdemo.greendao.dao.OperationProcessDataDao;
import com.lz.ccjdemo.greendao.dao.WarehouseDataDao;
import com.lz.ccjdemo.greendao.data.DishData;
import com.lz.ccjdemo.greendao.data.OperationProcessData;
import com.lz.ccjdemo.greendao.data.WarehouseData;

import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;

public class DaoUtils {
    DaoSession daoSession;
    DishDataDao dishDataDao;

    WarehouseDataDao warehouseDataDao;
    OperationProcessDataDao operationProcessDataDao;


    public DaoUtils() {
        daoSession = App.getApp().daoSession;
    }

    private static class LazyHolder {
        private static final DaoUtils INSTANCE = new DaoUtils();
    }

    public static DaoUtils getInstance() {
        return LazyHolder.INSTANCE;
    }

    private DishDataDao getDishDao() {
        if (dishDataDao == null) {
            dishDataDao = daoSession.getDishDataDao();
        }
        return dishDataDao;
    }

    private WarehouseDataDao getWarehouseDao(){
        if (warehouseDataDao==null){
        warehouseDataDao=    daoSession.getWarehouseDataDao();
        }
        return warehouseDataDao;
    }
    private OperationProcessDataDao getOperationProcessDao(){
        if (operationProcessDataDao==null){
            operationProcessDataDao=    daoSession.getOperationProcessDataDao();
        }
        return operationProcessDataDao;
    }
    /**
     * 插入、更新 DishData
     *
     * @param data 数据内容
     */
    public long insertDish(DishData data) {
       return getDishDao().insertOrReplace(data);
    }

    public long insertWarehouse(WarehouseData data) {
        return  getWarehouseDao().insertOrReplace(data);
    }

    public long insertOperationProcess(OperationProcessData data) {
        return getOperationProcessDao().insertOrReplace(data);
    }

    /**
     * 查詢Dish数据列表
     */
    public List<DishData> getDishAll() {
        List<DishData> dataList = getDishDao().loadAll();
        if (dataList == null) {
            dataList = new ArrayList<>();
        }
        return dataList;
    }

    /**
     *
     * @param cond DishDataDao.Properties.Dish_name.eq("")  查询完全满足条件的    Dish_name 是字段名称
     *             DishDataDao.Properties.Dish_name.notEq("") 查询除条件外的
     *             DishDataDao.Properties.Dish_name.like("xx%") 查询包含xx的
     * @param condMore  更多条件
     * @return
     */
    public List<DishData> queryEq(WhereCondition cond, WhereCondition... condMore){
        return getDishDao().queryBuilder().where(cond,condMore)
                .list();
    }

    //删除数据
    public  void delete(DishData data) {
        getDishDao().delete(data);
    }


    public void createTable(String tableName, List<WarehouseData> properties) {
        // 创建表结构


    }
}
